/*
 * Created on May 15, 2005
 *
 */
package org.mushroomdb.query.impl.condition;

import java.util.Calendar;
import java.util.Comparator;
import java.util.Date;

import org.mushroomdb.catalog.Column;
import org.mushroomdb.catalog.Register;
import org.mushroomdb.util.comparator.ComparatorFactory;


/**
 * @author Matias
 *
 */
public class GreatherThanCondition extends EvaluationCondition {

	/**
	 * @param column
	 * @param value
	 */
	public GreatherThanCondition(Column column, Object value) {
		super(column, value);
	}

	/**
	 * @see org.mushroomdb.query.impl.condition.EvaluationCondition#evaluate(com.mdlm.rdbms.catalog.Register)
	 */
	public boolean evaluate(Register register) {
		Column column = this.getColumn();
		Object value = this.getValue();
		
		Object left = register.getValue(register.getColumnIndex(column));
		Object right = null;
		if (value instanceof Column) {
			right = register.getValue(register.getColumnIndex((Column) value));
		} else {
			right = value;
		}
		
		if (left instanceof String) {
			Comparator comparator = ComparatorFactory.getInstance(String.class);
			return comparator.compare(left,right) > 0;
		} else if (left instanceof Long) {
			long leftInt = ((Long) left).longValue();
			long rightInt = ((Long) right).longValue();
			return leftInt > rightInt;
		} else if (left instanceof Integer) {
			int leftInt = ((Integer) left).intValue();
			int rightInt = ((Integer) right).intValue();
			return leftInt > rightInt;
		} else if (left instanceof Float) {
			float leftFloat = ((Float) left).floatValue();
			float rightFloat = ((Float) right).floatValue();
			return leftFloat > rightFloat;
		} else if (left instanceof Date) {
			Calendar calendar = Calendar.getInstance();
			calendar.setTime((Date) left);
			return calendar.after((Date) right);
		} else {
			return false;
		}
	}

	/**
	 * toString
	 */
	public String toString() {
		return this.getColumn().toString() + ">" + this.getValue().toString(); //$NON-NLS-1$
	}
}
